/**
 * @author Tsai, Min Che
 */
(function($) {
	$.platform = {
		event: {},
		addEvent: function() {
			for (i = 0; i < arguments.length; i++) {
				var options = arguments[i];
				if (typeof this.event[options.func] != 'object') {
					this.event[options.func] = {};
				}
				$.extend(this.event[options.func], options.event);
			}			
		},
		getEvent: function(func) {
			if (typeof this.event[func] == 'object') {
				return this.event[func];
			} else {
				return false;
				//alert('Event is not exists!');
			}
			return false;
		},
		removeEvent: function(func) {
			if (typeof this.event[func] == 'object') {
				this.event[func] = {};
			}
		},
		app: function(options) {
			if (typeof options.url != 'undefined') {
				url = options.url;
				args = url.substr(url.indexOf('?') + 1).split('&');
				for (var i in args) {
					args[i] = args[i].split('=');
					eval('options.' + args[i][0] + '="' + args[i][1] + '";');
				}
			}
			if (options.runat == 'client') {
				var event = this.getEvent(options.app + '_' + (options.sub_app != '' ? options.sub_app + '_' : '') + options.method);
				if (typeof event.init == 'function') {
					event.init(options.params);
				} else {
					options.runat = 'server';
					this.app(options);
				}
			} else {
				$.ajax({
					url: 'application.php?app=' + options.app + '&sub_app=' + options.sub_app + '&method=' + options.method + '&data_type=json',
					type: 'POST',
					data: options.params,
					dataType: 'json',
					success: function(result) {
						if (typeof result.Kui == 'object') {
							for (var i in result.Kui) {
								if (typeof eval('$.' + result.Kui[i].ui_name) == 'object') {
									eval('$.' + result.Kui[i].ui_name).init(result.Kui[i]);
								}
							}
						}
						if (typeof result.Scripts == 'object') {
							for (var i in result.Scripts) {
								$.getScript(result.Scripts[i]);
							}
						}
						if (typeof result.Exception == 'object') {
							$.platform.alert({
								title: 'Exception',
								content:
									'<table class="exception">' +
									'<tr><td>錯誤訊息</td><td class="message">'+ result.Exception.message + '</td></tr>' +
									'<tr><td>錯誤檔案</td><td class="file">' + result.Exception.file + '</td></tr>' +
									'<tr><td>錯誤行數</td><td class="line">第' + result.Exception.line + '行</td></tr>' +
									'</table>'
							});
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						if (XMLHttpRequest.responseText == '') {
							content = '沒有傳回任何值或傳回值為Null';
						} else {
							content = XMLHttpRequest.responseText;
						}
						$.platform.alert({
							title: 'PHP ERROR',
							content: content
						});
					}
				});
			}
		},
		alert: function(options) {
			$("#alert").html(options.content).dialog({
				title: options.title,
				width: '40%',
				height: $('#workspace').height() * 0.5,
				bgiframe: true,
				closeOnEscape: true,
				modal: true,
				buttons: {
					OK: function() {
						$(this).dialog('destroy');
					}
				}
			});
		}
	};
})(jQuery);